package com.boxGame.android;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.TextView;

public class Card extends FrameLayout {

	private int mNum;
	private TextView tvNum;
	private int mWidth, mHeight;
	private int []cardColor = {0x33ffffff, 0xffffffff, 0x33CCFF00, 0x11CC0000, 0x33CC0000, 0x44CC0000, 0x55CC0000, 0x66CC0000, 0x77CC0000, 0x88CC0000, 0x99CC0000};

	public Card(Context context) {
		super(context);
		initCard();
	}

	public Card(Context context, AttributeSet attrs) {
		super(context, attrs);
		initCard();
	}

	public Card(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initCard();
	}
	
	private void initCard() {
		
		tvNum = new TextView(getContext());
		tvNum.setTextSize(32);
		tvNum.setGravity(Gravity.CENTER);
		tvNum.setBackgroundColor(0x33ffffff);
		LayoutParams lp = new LayoutParams(-1, -1);
		lp.setMargins(25, 25, 0, 0);
		addView(tvNum, lp);
	}

	public int getNum() {
		return mNum;
	}

	public void setNum(int mNum) {
		this.mNum = mNum;
		if (mNum == 0) {
			tvNum.setText("");
		} else {
			tvNum.setText(mNum + "");
		}
		setBackground();
	}
	
	public void setBackground() {
		int colorNum = (int) Math.sqrt(mNum);
		if (colorNum >  cardColor.length - 1) {
			//��������Խ��
			colorNum = cardColor.length - 1;
		}
		tvNum.setBackgroundColor(cardColor[colorNum]);
	}
	
	public void startSwipeLeftAnima(int x, int y) {
		TranslateAnimation animation = new TranslateAnimation(x*mWidth, (x - 1)*mWidth, y*mHeight, y*mHeight);
		animation.setDuration(300);
		startAnimation(animation);
	}
	
	public boolean equals(Card o) {
		// TODO Auto-generated method stub
		return getNum() == o.getNum();
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		super.onSizeChanged(w, h, oldw, oldh);
		this.mWidth = w;
		this.mHeight = h;
		
		System.out.println("w:" + w + ",h:" + h);
	}
}
